﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SocialFront.Entities;
using SocialFront.Providers.Member;
using SocialFront.Providers.Relationship;

namespace SocialFront.Web.usercontrols.Gallery
{
    public partial class ILike : System.Web.UI.UserControl
    {
        public int ImageId { get; set; }
        public IList<Member> members { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            // Always get the members in the page load, regardless of postback or not
            members = RelationshipProvider.Provider.GetMembersILikeByImage(ImageId);

            if (!IsPostBack)
            {
                if (MemberProvider.Provider.GetCurrentMember() == null)
                {
                    lbILike.Visible = false;
                }
                else
                {
                    // TODO: Create an enum for relationship aliases
                    if (RelationshipProvider.Provider.RelationshipExists(MemberProvider.Provider.GetCurrentMember().Id,
                                                                         ImageId, "relateMemberILikeImage"))
                    {
                        lbILike.Text = "Liked";
                        lbILike.Enabled = false;
                    }
                }
            }
        }

        protected void lbILike_Click(object sender, EventArgs e)
        {
            RelationshipProvider.Provider.CreateMemberILikeAImage(
                    MemberProvider.Provider.GetCurrentMember().Id, ImageId);

            // now update the members so the correct total is displayed
            members = RelationshipProvider.Provider.GetMembersILikeByImage(ImageId);
            lbILike.Text = "Liked";
            lbILike.Enabled = false;
        }
    }
}